package com.kailong.service.Impl.sys;

import cn.hutool.core.bean.BeanUtil;
import com.kailong.dto.sys.MenuTreeDTO;
import com.kailong.entity.ResponsePageEntity;
import com.kailong.entity.sys.MenuConditionEntity;
import com.kailong.entity.sys.MenuEntity;
import com.kailong.entity.sys.MetaDTO;
import com.kailong.helper.TokenHelper;
import com.kailong.mapper.CustomerMapper;
import com.kailong.mapper.sys.MenuMapper;
import com.kailong.service.BaseService;
import com.kailong.service.sys.MenuService;
import com.kailong.util.AssertUtil;
import com.kailong.util.BetweenTimeUtil;
import com.kailong.util.ExcelUtil;
import com.kailong.util.FillUserUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
 * created by kailong on 2025/8/8
 */
@Service("menuService")
public class MenuServiceImpl extends BaseService<MenuEntity, MenuConditionEntity> implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private TokenHelper tokenHelper;

    @Override
    protected CustomerMapper getBaseMapper() {
        return menuMapper;
    }

    @Override
    public List<MenuTreeDTO> getMenuTree() {
        MenuConditionEntity menuConditionEntity = new MenuConditionEntity();
        menuConditionEntity.setPageSize(0);
        menuConditionEntity.setPid(0L);
        List<MenuEntity> menuEntities = menuMapper.searchByCondition(menuConditionEntity);
        if (CollectionUtils.isEmpty(menuEntities)) {
            return Collections.emptyList();
        }

        List<MenuTreeDTO> result = Lists.newArrayList();
        for (MenuEntity menuEntity : menuEntities) {
            MenuTreeDTO menuTreeDTO = buildMenuTreeDTO(menuEntity);
            menuTreeDTO.setAlwaysShow(true);
            result.add(menuTreeDTO);
            buildChildren(menuEntity, menuTreeDTO);
        }
        return result;
    }
    private void buildChildren(MenuEntity menuEntity, MenuTreeDTO menuTreeDTO) {
        MenuConditionEntity menuConditionEntity = new MenuConditionEntity();
        menuConditionEntity.setPageSize(0);
        menuConditionEntity.setPid(menuEntity.getId());
        List<MenuEntity> childrenEntities = menuMapper.searchByCondition(menuConditionEntity);
        if (CollectionUtils.isNotEmpty(childrenEntities)) {
            for (MenuEntity childrenEntity : childrenEntities) {
                MenuTreeDTO childrenMenuTreeDTO = buildMenuTreeDTO(childrenEntity);
                menuTreeDTO.addChildren(childrenMenuTreeDTO);
                buildChildren(childrenEntity, childrenMenuTreeDTO);
            }
        }
    }


    private MenuTreeDTO buildMenuTreeDTO(MenuEntity menuEntity) {
        MenuTreeDTO menuTreeDTO = BeanUtil.copyProperties(menuEntity, MenuTreeDTO.class);
        menuTreeDTO.setAlwaysShow(false);
        MetaDTO metaDTO = new MetaDTO();
        menuTreeDTO.setMeta(metaDTO);
        metaDTO.setIcon(menuTreeDTO.getIcon());
        metaDTO.setTitle(menuTreeDTO.getName());
        metaDTO.setNoCache(true);
        return menuTreeDTO;
    }
    public int deleteByIds(List<Long> ids) {
        List<MenuEntity> menuEntities = menuMapper.findByIds(ids);
        System.out.println(ids);
        AssertUtil.notEmpty(menuEntities, "菜单已被删除");
        MenuEntity menuEntity = new MenuEntity();
        tokenHelper.fillUpdateUserInfo(menuEntity);
        return menuMapper.deleteByIds(ids, menuEntity);

    }


    @Override
    public ResponsePageEntity<MenuEntity> searchByPage(MenuConditionEntity menuConditionEntity) {
        // 实现分页查询逻辑
        List<MenuEntity> menuList = menuMapper.searchByCondition(menuConditionEntity);
        int totalCount = menuMapper.countByCondition(menuConditionEntity);

        // 计算总页数
        int pageSize = menuConditionEntity.getPageSize() != null ? menuConditionEntity.getPageSize() : 10;
        int totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        int pageNo = menuConditionEntity.getPageNo() != null ? menuConditionEntity.getPageNo() : 1;
        BetweenTimeUtil.parseTime(menuConditionEntity);
        return new ResponsePageEntity<>(pageNo, pageSize, totalPage, totalCount, menuList);
    }

//    @Override
//    public void export(HttpServletResponse response, MenuConditionEntity menuConditionEntity) throws IOException {
//        menuConditionEntity.setPageSize(0);
//        List<MenuEntity> menuEntities = menuMapper.searchByConditionWithoutPage(menuConditionEntity);
//        ExcelUtil.exportExcel("菜单数据", MenuEntity.class, menuEntities, response);
//        BetweenTimeUtil.parseTime(menuConditionEntity);
//    }
    @Override
    public int insert(MenuEntity menuEntity) {
        menuEntity.setCreateUserId(12L);
        menuEntity.setCreateUserName("admin");
        FillUserUtil.fillUpdateUserInfo(menuEntity);
        return menuMapper.insert(menuEntity);
    }
    @Override
    public int update(MenuEntity menuEntity) {
        AssertUtil.notNull(menuEntity.getId(), "菜单ID不能为空");
        FillUserUtil.fillUpdateUserInfo(menuEntity);
        System.out.println(menuEntity.getSort());
        return menuMapper.update(menuEntity);
    }
}
